package com.zhangbo.share.dubbo.filter;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.springframework.stereotype.Component;

/**
 * @author zhangbo
 * @version 1.00
 * @classdesc <pre></pre>
 * @create 2021/12/19 14:31
 * @update <pre>
 *     版本           修改人         修改日期         修改内容描述
 *     -----------------------------------------------------------
 *     1.00           zhangbo     2021/12/19 14:31
 *     -----------------------------------------------------------
 * </pre>
 */
@Slf4j
@Component
@Activate(group = "provider")
public class ProviderFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        log.info("dubbo调用请求参数：{}", invocation.getArguments());
        Result invoke = null;
        invoke = invoker.invoke(invocation);
        if (null == invoke.getValue() && null != invoke.getException()) {
            log.error("dubbo调用异常，请求参数：{}\nerror：{}", invocation.getArguments(), invoke.getException());
            invoke.setAttachment("exception", invoke.getException().getMessage());
        } else {
            log.info("dubbo调用请求参数：{}，响应参数：{}", invocation.getArguments(), invoke.getValue());
        }

        return invoke;
    }

}
